package com.questionnaire.modular.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.questionnaire.common.base.R;
import com.questionnaire.common.base.ShiroUser;
import com.questionnaire.common.base.SimpleToken;
import com.questionnaire.common.redis.RedisUtil;
import com.questionnaire.constant.ShiroConstant;

import com.questionnaire.core.impl.JwtTokenManager;
import com.questionnaire.core.shiro.custom.CustomUsernamePwdToken;
import com.questionnaire.modular.system.dao.UserMapper;
import com.questionnaire.modular.system.model.Menu;
import com.questionnaire.modular.system.model.MenuNode;
import com.questionnaire.modular.system.model.User;
import com.questionnaire.modular.system.service.MenuService;
import com.questionnaire.modular.system.service.RoleService;
import com.questionnaire.modular.system.service.UserService;
import com.questionnaire.utils.ApiMenuFilter;
import com.questionnaire.utils.ShiroKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: YSL
 * @time: 2023/11/9 11:33
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private JwtTokenManager jwtTokenManager;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public R login(@RequestBody User user) {
        //实现登录
        String jwtToken = null;
        R<Object> result = new R<>();
        try {
//            SimpleToken simpleToken = new SimpleToken(null, user.getUserName(), user.getPassWord());
            CustomUsernamePwdToken token = new CustomUsernamePwdToken(user.getUserName(), user.getPassWord());
//            登录
            Subject subject = ShiroKit.getSubject();
            subject.login(token);
//            成功颁发令牌
            ShiroUser shiroUser = ShiroKit.getUser();
            String sessionId = (String) ShiroKit.getSession().getId();
//           创建 claims
            Map<String,Object> claims = new HashMap<>();
            claims.put("shiroUser", JSONObject.toJSONString(shiroUser));
            jwtToken = jwtTokenManager.issuedToken("system", subject.getSession().getTimeout(), sessionId, claims);
//            String redisKey="account_"+user.getUserName();
////            redis 缓存 token信息
//            redisUtil.setObjRedis(redisKey,jwtToken);
             result = R.success(ShiroConstant.LOGIN_SUCCESS_CODE, ShiroConstant.LOGIN_SUCCESS_MESSAGE,"",jwtToken);
        }catch (Exception e){
            System.out.println(e);
//            登录失败
             result = R.error(ShiroConstant.LOGIN_FAILURE_CODE, ShiroConstant.LOGIN_FAILURE_MESSAGE);
        }
        return result;
    }
    /**
     *
     * <b>方法名：</b>：loadAuthorityToCache<br>
     * <b>功能说明：</b>：加载缓存<br>
     */
//    private void loadAuthorityToCache(SimpleToken token){
//        //登陆成功后缓存用户的权限信息进入缓存
//        ShiroUser shiroUser = ShiroKit.getUser();
//        userBridgeService.loadUserAuthorityToCache(shiroUser);
//    }
}
